package com.hanlin.backend.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hanlin.backend.common.util.RedisUtil;
import com.hanlin.backend.common.util.ResultCommon;
import com.hanlin.backend.common.util.TableCommon;
import com.hanlin.backend.dto.RoleDto;
import com.hanlin.backend.service.HlRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author hl.yuan
 * @since 2021-01-06
 */
@RestController
@Slf4j
@Api(value = "/frontend/api/role", tags = "角色")
public class HlRoleController {

    @Autowired
    private HlRoleService hlRoleService;

    @Autowired
    private RedisUtil redisUtil;

    @GetMapping("/frontend/api/role/tableList")
    @ApiOperation("获取角色列表(带分页)")
    public ResultCommon<TableCommon<RoleDto>> getTableList(RoleDto dto) {
        IPage<RoleDto> role = hlRoleService.getTableRoleList(dto);
        return dto.build(role);
    }

    @GetMapping("/frontend/api/role/roleList")
    @ApiOperation("获取角色列表")
    public ResultCommon<List<RoleDto>> getRoleList(Integer roleId, String roleName) {
        ResultCommon common = new ResultCommon();
        try {
            List<RoleDto> role = hlRoleService.getRoleList(roleId, roleName);
            common.setData(role);
        } catch (Exception ex) {
            log.error("获取角色列表失败", ex);
            common.setCode("500");
        }
        return common;
    }

    @GetMapping("/frontend/api/role/getRoleAndUser")
    @ApiOperation("获取角色列表，且包含用户")
    public ResultCommon getRoleAndUser(){
        ResultCommon common = new ResultCommon();
        try {
            List<Map> role = hlRoleService.getRoleAndUser();
            common.setData(role);
        } catch (Exception ex) {
            log.error("获取角色列表，且包含用户失败", ex);
            common.setCode("500");
        }
        return common;
    }


}

